package com.process.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.outer.CompanyDetailVoDao;
import com.process.dao.outer.CompanyEvaluationVoDao;
import com.process.dao.outer.CompanyVoDao;
import com.process.domain.outer.CompanyEvaluationVo;
import com.process.domain.outer.OutTeacher;
import com.process.domain.publi.Company;
import com.process.service.out.OutTeacherService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description:
 * @ClassName: CompanyController
 * @Author: 殷凡卓
 * @Date: 2023/4/26 17:33
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private OutTeacherService outTeacherService;
    @Autowired
    private CompanyDetailVoDao companyDetailVoDao;
    @Autowired
    private CompanyEvaluationVoDao companyEvaluationVoDao;
    @Autowired
    private CompanyVoDao companyVoDao;

    @SneakyThrows
    @PostMapping("/import_teacher")
    public R importPracticalTask(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        //注意 这里的参数 是List集合中的实体类型
        List<OutTeacher> list = reader.readAll(OutTeacher.class);
        //批量插入
        list.forEach(e -> {
            System.out.println(e);
        });
        outTeacherService.saveBatch(list);
        return new R(Code.SAVE_OK);
    }


    //李春---------------------------------------------------------------
    @GetMapping("/companyDetail")
    public R getcompanyDetailByCompanyPhone(ServletRequest servletRequest) {
        String phone = (String) servletRequest.getAttribute("username");
        Company company = companyDetailVoDao.selectCompanyDetailByPhone(phone);
        return new R(Code.GET_OK, company, "获取我的详细信息");
    }

    @GetMapping("/allStuEvaluationVo")
    public R getallStuEvaluationVo(ServletRequest servletRequest) {

        String phone = (String) servletRequest.getAttribute("username");
        List<CompanyEvaluationVo> companyEvaluationVos = companyEvaluationVoDao.selectAllStudentEvaluationByCompanyPhone(phone);
        return new R(Code.GET_OK, companyEvaluationVos, "获取所有学生对你的评价");
    }

    @GetMapping("/allCompany")
    public R getallCompany(ServletRequest servletRequest) {
        List<Company> companies = companyVoDao.selectAllCompany();
        return new R(Code.GET_OK, companies, "获取我的详细信息");
    }

    //@RequestParam("token") String token
//    @SneakyThrows
//    @PostMapping("/import_OutTeacher")
//    public R import_Teacher(
//            @RequestParam("file") MultipartFile file,
//            HttpServletRequest request) throws IOException {
//        InputStream inputStream = file.getInputStream();
//        ExcelReader reader = ExcelUtil.getReader(inputStream);
//        //注意 这里的参数 是List集合中的实体类型
//        List<Teacher> list = reader.readAll(Teacher.class);
//        //批量插入
//        list.forEach(e -> {
//            System.out.println(e);
//        });
//        teacherService.saveBatch(list);
//        return new R(Code.SAVE_OK);
//    }
}
